home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / nt / source.exe / POSIX / FIND / LS.C < prev    next >
C/C++ Source or Header  |  1992-10-29  |  5KB  |  163 lines

  1. /*
  2.  * Copyright (c) 1989 The Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  * 1. Redistributions of source code must retain the above copyright
  9.  *    notice, this list of conditions and the following disclaimer.
  10.  * 2. Redistributions in binary form must reproduce the above copyright
  11.  *    notice, this list of conditions and the following disclaimer in the
  12.  *    documentation and/or other materials provided with the distribution.
  13.  * 3. All advertising materials mentioning features or use of this software
  14.  *    must display the following acknowledgement:
  15.  *    This product includes software developed by the University of
  16.  *    California, Berkeley and its contributors.
  17.  * 4. Neither the name of the University nor the names of its contributors
  18.  *    may be used to endorse or promote products derived from this software
  19.  *    without specific prior written permission.
  20.  *
  21.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  22.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  25.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  27.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  30.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31.  * SUCH DAMAGE.
  32.  */
  33.  
  34. #ifndef lint
  35. static char sccsid[] = "@(#)ls.c    5.6 (Berkeley) 3/9/91";
  36. #endif /* not lint */
  37.  
  38. #ifdef DF_POSIX    /* DF_MSS */
  39. #include <misc.h>
  40. #include <bsdlib.h>
  41. #include <sys/cdefs.h>
  42. #else
  43. #include <sys/param.h>
  44. #endif
  45. #include <sys/stat.h>
  46. #include <time.h>
  47. #include <tzfile.h>
  48. #include <errno.h>
  49. #include <utmp.h>
  50. #include <unistd.h>
  51. #include <stdio.h>
  52. #include <string.h>
  53. #if WIN_NT
  54.  
  55. extern char *group_from_gid __P((gid_t, int));
  56. extern char *user_from_uid __P((uid_t, int));
  57. #endif
  58.  
  59. void printtime __P((time_t));
  60.  
  61. /* Derived from the print routines in the ls(1) source code. */
  62.  
  63. void
  64. #if __STDC__
  65. printlong (char *name, char *accpath, struct stat *sb)
  66. #else
  67. printlong(name, accpath, sb)
  68.     char *name;            /* filename to print */
  69.     char *accpath;            /* current valid path to filename */
  70.     struct stat *sb;        /* stat buffer */
  71. #endif
  72. {
  73. #if WIN_NT
  74.     char modep[15];
  75. #else
  76.     char modep[15], *user_from_uid(), *group_from_gid();
  77. #endif
  78.  
  79. #ifdef _POSIX_SOURCE /* DF_DSC */
  80.     (void)printf("%6lu %4ld ", sb->st_ino, ((sb->st_size/BLOCK_SIZE) + 1));
  81. #else
  82.     (void)printf("%6lu %4ld ", sb->st_ino, sb->st_blocks);
  83. #endif
  84.     (void)strmode(sb->st_mode, modep);
  85.     (void)printf("%s %3u %-*s %-*s ", modep, sb->st_nlink, UT_NAMESIZE,
  86.         user_from_uid(sb->st_uid, 0), UT_NAMESIZE,
  87.         group_from_gid(sb->st_gid, 0));
  88.  
  89. #ifdef _POSIX_SOURCE /* DF_DSC: No devices for the moment in Windows NT POSIX */
  90.         (void)printf("%8ld ", sb->st_size);
  91. #else
  92.     if (S_ISCHR(sb->st_mode) || S_ISBLK(sb->st_mode))
  93.         (void)printf("%3d, %3d ", major(sb->st_rdev),
  94.             minor(sb->st_rdev));
  95.     else
  96.         (void)printf("%8ld ", sb->st_size);
  97. #endif
  98.     printtime(sb->st_mtime);
  99.     (void)printf("%s", name);
  100. #ifdef _POSIX_SOURCE /* DF_DSC: softlinks not in Windows NT POSIX */
  101.     if (accpath == NULL)
  102.         ;
  103. #else
  104.     if (S_ISLNK(sb->st_mode))
  105.         printlink(accpath);
  106. #endif
  107.     (void)putchar('\n');
  108. }
  109.  
  110. void
  111. #if __STDC__
  112. printtime (time_t ftime)
  113. #else
  114. printtime(ftime)
  115.     time_t ftime;
  116. #endif
  117. {
  118.     int i;
  119. #if WIN_NT
  120.     char *longstring;
  121. #else
  122.     char *longstring, *ctime();
  123.     time_t time();
  124. #endif
  125.  
  126.     longstring = ctime((long *)&ftime);
  127.     for (i = 4; i < 11; ++i)
  128.         (void)putchar(longstring[i]);
  129.  
  130. #define    SIXMONTHS    ((DAYSPERNYEAR / 2) * SECSPERDAY)
  131.     if (ftime + SIXMONTHS > time((time_t *)NULL))
  132.         for (i = 11; i < 16; ++i)
  133.             (void)putchar(longstring[i]);
  134.     else {
  135.         (void)putchar(' ');
  136.         for (i = 20; i < 24; ++i)
  137.             (void)putchar(longstring[i]);
  138.     }
  139.     (void)putchar(' ');
  140. }
  141.  
  142. #ifdef _POSIX_SOURCE /* DF_DSC: softlinks not in Windows NT POSIX */
  143. #else
  144. # if __STDC__
  145. printlink (char *name)
  146. # else
  147. printlink(name)
  148.     char *name;
  149. # endif
  150. {
  151.     int lnklen;
  152.     char path[MAXPATHLEN + 1];
  153.  
  154.     if ((lnklen = readlink(name, path, MAXPATHLEN)) == -1) {
  155.         (void)fprintf(stderr,
  156.             "\nfind: %s: %s\n", name, strerror(errno));
  157.         return;
  158.     }
  159.     path[lnklen] = '\0';
  160.     (void)printf(" -> %s", path);
  161. }
  162. #endif
  163.